package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sgb.knowledgeshare.R;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * //MyJCVideoPlayer
 * description 全屏播放Videoview
 */
public class FullScreenVideoView2 extends JCVideoPlayerStandard {

    private ImageView mThumbImage;
    private ImageView mStartImage;
    private AlphaAnimation alphaAniHide;

    public FullScreenVideoView2(Context context) {
        super(context);
    }

    public FullScreenVideoView2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void init(Context context) {
        super.init(context);
        LinearLayout linearLayout = findViewById(R.id.layout_bottom);
        linearLayout.setVisibility(GONE);
        loop = true;//循环播放时true、不循环播放时false
    }

    @Override
    public void onClick(View v) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        super.onClick(v);
    }

    @Override
    public void setUiWitStateAndScreen(int state) {
        super.setUiWitStateAndScreen(state);
        startButton.setVisibility(GONE);
        topContainer.setVisibility(GONE);
        bottomContainer.setVisibility(GONE);
        backButton.setVisibility(GONE);
    }

    public void setVideoPath(String coverURL) {
        setUp(coverURL, JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, "");
        startButton.setVisibility(GONE);
    }

    public void setThumbImage(ImageView thumbImage, ImageView startImage) {
        mThumbImage = thumbImage;
        mStartImage = startImage;
        mThumbImage.setVisibility(VISIBLE);
        mStartImage.setVisibility(GONE);
    }

    public void start() {
        if (currentState != CURRENT_STATE_PLAYING) {
            startButton.performClick();
        }
        if (mStartImage != null) {
            mStartImage.setVisibility(GONE);
        }
    }

    public void pauseCurrentVideo() {
        if (currentState == CURRENT_STATE_PLAYING) {
            startButton.performClick();
        }
        if (mStartImage != null) {
            mStartImage.setVisibility(VISIBLE);
        }
    }

    public void pause() {
        if (currentState == CURRENT_STATE_PLAYING) {
            startButton.performClick();
        }
        if (mStartImage != null) {
            mStartImage.setVisibility(VISIBLE);
        }
    }

    @Override
    public void setProgressAndText() {
        super.setProgressAndText();
        if (isPlaying()&&mStartImage.getVisibility() == VISIBLE){
            mStartImage.setVisibility(GONE);
        }
        if (isPlaying()&&mThumbImage.getVisibility() == VISIBLE){
            mThumbImage.setVisibility(GONE);
        }

    }

    @Override
    public void onCompletion() {
        if (mThumbImage != null) {
            mThumbImage.setVisibility(VISIBLE);
        }
        if (mStartImage != null) {
            mStartImage.setVisibility(VISIBLE);
        }
        super.onCompletion();
    }

    public void stopPlayback() {
        JCVideoPlayer.releaseAllVideos();
    }

    public boolean isPlaying() {
        return currentState == CURRENT_STATE_PLAYING;
    }

    public void setGoneView() {
        findViewById(R.id.surface_container).setVisibility(GONE);
        findViewById(R.id.start).setVisibility(GONE);
    }
}
